<template>
  <div>
    <div class="action-bar">
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
    </div>
    <FormDialog
      title="新建"
      :show="dialogFormVisible"
      width="40%"
      @on-ok="ok"
      @on-cancel="cancel"
    >
      <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
        <el-form-item label="业务模型标识" prop="code" required>
          <el-input v-model="formItem.code" placeholder="请输入唯一标识" />
        </el-form-item>
        <el-form-item label="业务模型名" prop="name" required>
          <el-input v-model="formItem.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formItem.desc" placeholder="请输入业务模型描述" />
        </el-form-item>
      </el-form>
    </FormDialog>
  </div>
</template>

<script>
import { create } from '@neu/cmdb-ui/api/bizdef'
import FormDialog from '@neu/biz-core/components/FormDialog'

const formItem = {
  code: '',
  name: '',
  desc: ''
}

export default {
  name: 'BizDefActionBar',
  components: {
    FormDialog
  },
  data() {
    return {
      dialogFormVisible: false,
      formItem: { ...formItem },
      // 表单验证规则
      rules: {
        code: [
          { required: true, message: '唯一标识不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '业务模型名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新建用户按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建用户对话框确认事件处理
    ok() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          create(this.formItem).then(response => {
            this.$parent.refresh()
            // const _id = response.data._id
            // this.$router.push({ name: 'cmdb_schemas_edit', params: { id: _id }})
            this.dialogFormVisible = false
          })
        }
      })
    },
    cancel() {
      this.$refs.myform.resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>
